package com.leixiaoqiao.controller;

import com.github.pagehelper.PageInfo;
import com.leixiaoqiao.pojo.Customer;
import com.leixiaoqiao.service.impl.CustomerServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@WebServlet(urlPatterns = {"/customer"},
        initParams = {
        @WebInitParam(name = "pageNo",value ="1"),
        @WebInitParam(name = "pageSize",value = "5")
        })
public class CustomerController extends BaseServlet {
    //展示所有数据
    public void showList(HttpServletRequest req, HttpServletResponse resp) {
        //创建业务层实例
        CustomerServiceImpl customerService = new CustomerServiceImpl();
        //获取分页传递过来的数据
        String pageNo = req.getParameter("pageNo");
        String pageSize = req.getParameter("pageSize");
        //判断用户是否传递了页码与当前页的记录数
        if(Objects.isNull(pageNo) || "".equals(pageNo)){
            pageNo = this.getInitParameter("pageNo");
        }
        if(Objects.isNull(pageSize) || "".equals(pageSize)){
            pageSize = this.getInitParameter("pageSize");
        }

        //获取条件查询的参数
        String name = req.getParameter("name");
        String idnumber = req.getParameter("idnumber");
        String sex = req.getParameter("sex");
        String city = req.getParameter("city");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        //将请求参数交予业务层
        //将条件查询的参数封装到Map中，将Map传递业务层实现分页处理
        HashMap<String, String> queryParamMap = new HashMap<>();
        queryParamMap.put("name",name);
        queryParamMap.put("idnumber",idnumber);
        queryParamMap.put("sex",sex);
        queryParamMap.put("city",city);
        queryParamMap.put("phone",phone);
        queryParamMap.put("email",email);

        queryParamMap.put("pageNo",pageNo);
        queryParamMap.put("pageSize",pageSize);
        //调用业务层方法获取数据集合
        PageInfo<Customer> pageInfo = customerService.findPage(queryParamMap);
        //将数据放到域中，交给视图层
        req.setAttribute("queryMap",queryParamMap);
        req.setAttribute("pageInfo", pageInfo);
        //转发到数据展示页面
        try {
            req.getRequestDispatcher("/page/show.jsp").forward(req, resp);
        } catch (ServletException | IOException e) {
            System.out.println("客户控制器数据展示请求转发异常！");
            e.printStackTrace();
        }
    }
    //根据编号删除客户信息
    public void deleteCustomerById(HttpServletRequest req, HttpServletResponse resp) {
        //创建业务层实例
        CustomerServiceImpl customerService = new CustomerServiceImpl();
        //获取前端传来的id
        String id = req.getParameter("id");
        try {
            customerService.deleteCustomerById(Integer.parseInt(id));
            //重新显示列表页面
            resp.sendRedirect(req.getContextPath() + "/customer?method=showList");
        } catch (Exception e) {
            System.out.println("删除控制器异常！");
            e.printStackTrace();
        }
    }
    //根据编号获取当前客户信息
    public void getCustomerById(HttpServletRequest req, HttpServletResponse resp){
        //创建业务层实例
        CustomerServiceImpl customerService = new CustomerServiceImpl();
        //获取前端传来的id
        String id = req.getParameter("id");
        try {
            Customer customerById = customerService.findCustomerById(Integer.parseInt(id));
            //将拿到的值放到域中，并将请求转发到修改页面
            req.setAttribute("cById",customerById);
            req.getRequestDispatcher("/page/edit.jsp").forward(req,resp);
        }catch (Exception e){
            System.out.println("根据Id查找客户控制器异常！");
            e.printStackTrace();
        }
    }
    //根据唯一id，修改其对应客户信息
    public void updateCustomerByKey(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException {
        //获取所有前端发送过来的数据
        Map<String, String[]> parameterMap = req.getParameterMap();
        Customer customer = new Customer();
        //调用方法将map里面的数据封装到JavaBean(customer)中
        BeanUtils.populate(customer,parameterMap);
        //创建业务类的实例
        CustomerServiceImpl customerService = new CustomerServiceImpl();
        try {
            customerService.updateCustomer(customer);
            resp.sendRedirect(req.getContextPath()+"/customer?method=showList");
        } catch (Exception e) {
            System.out.println("控制层调用修改操作异常！");
            e.printStackTrace();
        }
    }
    //添加客户
    public void addCustomer(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        Customer customer = new Customer();
        BeanUtils.populate(customer,parameterMap);
        CustomerServiceImpl customerService = new CustomerServiceImpl();
        try {
            customerService.addCustomer(customer);
            resp.sendRedirect(req.getContextPath()+"/customer?method=showList");
        } catch (Exception e) {
            System.out.println("控制层调用添加操作异常！");
            e.printStackTrace();
        }
    }
    //批量删除
    public void deleteByIds(HttpServletRequest req, HttpServletResponse resp){
        //获取批量删除的id
        String[] ids = req.getParameterValues("id");
        CustomerServiceImpl customerService = new CustomerServiceImpl();
        try {
            customerService.deleteCustomers(ids);
            resp.sendRedirect(req.getContextPath()+"/customer?method=showList");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("控制器批量删除发生异常！");
        }
    }

}
